<template>
  <div :class="className" :style="{height:height,width:width,color:color}" />
</template>

<script>
import echarts from 'echarts'
import resize from '../../views/dashboard/admin/components/mixins/resize'

const handred = 100

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    color: {
      type: String,
      default: 'red'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions({ point, color } = {}) {
      this.chart.setOption({
        title: {
          text: point + '%',
          x: 'center',
          y: 'center',
          textStyle: {
            fontWeight: 'normal',
            color: color,
            fontSize: '25'
          }
        },
        tooltip: {
          formatter: function(params) {
            return params.name + '：' + params.percent + ' %'
          }
        },
        series: [{
          name: 'circle',
          type: 'pie',
          radius: ['50%', '66%'],
          itemStyle: {
            normal: {
              label: {
                show: false
              },
              labelLine: {
                show: false
              }
            }
          },
          hoverAnimation: false,
          data: [{
            value: point,
            name: '占比',
            itemStyle: {
              normal: {
                color: '#E9E5E4'
              },
              label: {
                show: false
              },
              labelLine: {
                show: false
              }
            }
          }, {
            name: '剩余',
            value: handred - point,
            itemStyle: {
              normal: {
                color: '#676463'
              }
            }
          }]
        }]
      })
    }
  }
}
</script>
